package com.example.config;

import com.example.domain.Message;
import com.example.service.SseService;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Configuration
public class SendMessageTask {
    private SseService sseService;
    public SendMessageTask(SseService sseService) {
        this.sseService = sseService;
    }
    @Scheduled(cron = "*/5 * * * * *") // 每5秒执行一次
    public void sendMessage() {
        Message message = new Message();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        message.setData(LocalDateTime.now().format(dateTimeFormatter));
        sseService.sendMessage(message);
    }
}
